package br.com.megasena.testes;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import br.com.megasena.aplicacao.AdministradorSorteios;
import br.com.megasena.dominio.Sorteio;

public class ImportadorMegaSenaHtml {

	private List<String> fileList;
	private static final String INPUT_ZIP_FILE = "C:\\MyFile.zip";
	private static final String OUTPUT_FOLDER = "/tmp/";
	private static final Integer TIMEOUT_SOCKET = 600;

	private static AdministradorSorteios administradorSorteios = new AdministradorSorteios();

	/**
	 * Unzip it
	 * 
	 * @param zipFile
	 *            input zip file
	 * @param output
	 *            zip file output folder
	 */
	public static void unZipIt(String zipFile, String outputFolder) {

		byte[] buffer = new byte[1024];

		try {

			// create output directory is not exists
			File folder = new File(OUTPUT_FOLDER);
			if (!folder.exists()) {
				folder.mkdir();
			}

			// get the zip file content
			ZipInputStream zis = new ZipInputStream(
					new FileInputStream(zipFile));
			// get the zipped file list entry
			ZipEntry ze = zis.getNextEntry();

			while (ze != null) {

				String fileName = ze.getName();
				File newFile = new File(outputFolder + File.separator
						+ fileName);

				System.out.println("file unzip : " + newFile.getAbsoluteFile());

				// create all non exists folders
				// else you will hit FileNotFoundException for compressed folder
				new File(newFile.getParent()).mkdirs();

				FileOutputStream fos = new FileOutputStream(newFile);

				int len;
				while ((len = zis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}

				fos.close();
				ze = zis.getNextEntry();
			}

			zis.closeEntry();
			zis.close();

			System.out.println("Done");

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static void download(String address, String localFileName) {
		OutputStream out = null;
		URLConnection conn = null;
		InputStream in = null;

		try {
			URL url = new URL(address);
			out = new BufferedOutputStream(new FileOutputStream(localFileName));
			conn = url.openConnection();
			in = conn.getInputStream();
			byte[] buffer = new byte[1024];

			int numRead;
			long numWritten = 0;

			while ((numRead = in.read(buffer)) != -1) {
				out.write(buffer, 0, numRead);
				numWritten += numRead;
			}

			System.out.println(localFileName + "\t" + numWritten);
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException ioe) {
			}
		}
	}

	public static void download(String address) {
		int lastSlashIndex = address.lastIndexOf('/');
		if (lastSlashIndex >= 0 && lastSlashIndex < address.length() - 1) {
			download(address, address.substring(lastSlashIndex + 1));
		} else {
			System.err.println("Could not figure out local file name for "
					+ address);
		}
	}

	/*
	 * public void DownloadDatabase(String DownloadUrl, String fileName) { try {
	 * File root = android.os.Environment.getExternalStorageDirectory(); File
	 * dir = new File(root.getAbsolutePath() + "/timy/databases");
	 * if(dir.exists() == false){ dir.mkdirs(); }
	 * 
	 * URL url = new URL("http://myexample.com/android/timy.zip"); File file =
	 * new File(dir,fileName);
	 * 
	 * long startTime = System.currentTimeMillis();
	 * System.out.println("DownloadDatabase " + "download url:" +url);
	 * System.out.println("DownloadDatabase " + "download file name:" +
	 * fileName);
	 * 
	 * URLConnection uconn = url.openConnection();
	 * uconn.setConnectTimeout(TIMEOUT_SOCKET);
	 * 
	 * InputStream is = uconn.getInputStream();
	 * 
	 * ZipInputStream zipinstream = new ZipInputStream(new
	 * BufferedInputStream(is)); ZipEntry zipEntry;
	 * 
	 * while((zipEntry = zipinstream.getNextEntry()) != null){ String
	 * zipEntryName = zipEntry.getName(); File file1 = new File(file +
	 * zipEntryName); if(file1.exists()){
	 * 
	 * }else{ if(zipEntry.isDirectory()){ file1.mkdirs(); } } }
	 * 
	 * BufferedInputStream bufferinstream = new BufferedInputStream(is);
	 * 
	 * ByteArrayBuffer baf = new ByteArrayBuffer(5000); int current = 0;
	 * while((current = bufferinstream.read()) != -1){ baf.append((byte)
	 * current); }
	 * 
	 * FileOutputStream fos = new FileOutputStream( file);
	 * fos.write(baf.toByteArray()); fos.flush(); fos.close();
	 * System.out.println("DownloadDatabase " + "download ready in" +
	 * ((System.currentTimeMillis() - startTime)/1000) + "sec"); }
	 * catch(IOException e) { System.out.println("DownloadDatabase " + "Error:"
	 * + e); e.printStackTrace(); }
	 * 
	 * }
	 */
	
	public static File baixarArquivo(String urlToDownload, String pathToFile){
		//urlToDownload = "http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip";
		//urlToDownload = "http://localhost/D_megase.zip";
		//pathToFile = "/tmp/D_MEGA.HTM";
		//String dir = "/tmp";
		//String fileName = "D_MEGA.HTM";

		URL url;
		//File file = new File(dir, fileName);
		File file = new File(pathToFile);
		try {
			url = new URL(urlToDownload);
			URLConnection uconn;
			try {
				System.out.println("Baixando: " + urlToDownload);

				// Proxy proxy = new Proxy(Proxy.Type.HTTP, new
				// InetSocketAddress("127.0.0.1", 3128));
				// uconn = url.openConnection(proxy);
				uconn = url.openConnection();

				// uconn.setConnectTimeout(TIMEOUT_SOCKET);

				InputStream is = uconn.getInputStream();

				System.out.println("ok");

				ZipInputStream zipinstream = new ZipInputStream(
						new BufferedInputStream(is));
				ZipEntry zipEntry;

				while ((zipEntry = zipinstream.getNextEntry()) != null) {
					String zipEntryName = zipEntry.getName();
					System.out.println("zipEntryName: " + zipEntryName);
					if (zipEntryName.equals("D_MEGA.HTM")) {
						//File file1 = new File(dir + "/" + zipEntryName);
						File file1 = new File(pathToFile);
						if (file1.exists()) {
							System.out.println("Arquivo [" + file1 + "] já existe!");
						} else {
							System.out.println("Arquivo encontrado!" + file1);
							if (zipEntry.isDirectory()) {
								file1.mkdirs();
							} else {
								byte[] data = new byte[1000];
								int byteRead;

								BufferedOutputStream bout = null;
								byteRead = 0;
								data = new byte[1000];
								// bout = new BufferedOutputStream(new
								// FileOutputStream(zipEntry.getName()),1000);
								bout = new BufferedOutputStream(
										new FileOutputStream(file1), 1000);
								while ((byteRead = zipinstream.read(data, 0,
										1000)) != -1) {
									bout.write(data, 0, byteRead);
								}
								bout.flush();
								bout.close();
							}
						}
					}
				}
				/*
				 * BufferedInputStream bufferinstream = new
				 * BufferedInputStream(is);
				 * 
				 * try { byte[] data = new byte[1000]; int byteRead;
				 * 
				 * BufferedOutputStream bout = null; //ZipInputStream zin = new
				 * ZipInputStream(new BufferedInputStream(new
				 * FileInputStream("my.zip"))); ZipInputStream zin = new
				 * ZipInputStream(new BufferedInputStream(is)); ZipEntry entry;
				 * while((entry = zin.getNextEntry()) != null) { byteRead=0;
				 * data = new byte[1000]; bout = new BufferedOutputStream(new
				 * FileOutputStream(entry.getName()),1000); while ((byteRead =
				 * zin.read(data,0,1000)) != -1) { bout.write(data,0,byteRead);
				 * } bout.flush(); bout.close(); } } catch(Exception e) {
				 * e.printStackTrace(); }
				 */
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		} catch (MalformedURLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		return file;
	}

	public static void main(String[] args) {
		/*
		for (int i = 0; i < args.length; i++) {
			download(args[i]);
		}
		 */
		
		String urlToDownload = "http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip";
		urlToDownload = "http://localhost/D_megase.zip";
		String filepath = "D_MEGA.HTM";
		File fileHandle = baixarArquivo(urlToDownload,filepath);

		// unZipIt(urlToDownload,"/tmp/D_MEGA.HTM");

		//String fileName = "/home/tobq/workspace/megasena/D_megase/D_MEGA.HTM";
		//File fileHandle = new File(fileName);
		if (fileHandle.exists()) {
			System.out.println("Arquivo ["+fileHandle+"] existe!");
			
			
			String charsetName = "ISO-8859-1";//charsetName = "UTF-8";

			try {
				Document doc1 = Jsoup.parse(fileHandle, charsetName);
				Elements tableElements = doc1.select("table");
				//System.out.println("Tabela: " + tableElements);

				//Elements tableHeaderEles = tableElements.select("thead tr th");
				Elements tableHeaderEles = tableElements.select(":not(tbody) tr th");
				System.out.println("Cabeçalho: " + tableHeaderEles);
				for (int i = 0; i < tableHeaderEles.size(); i++) {
					System.out.println(tableHeaderEles.get(i).text());
				}
				System.out.println();

				//Elements tableRowElements = tableElements.select(":not(thead) tr");
				Elements tableRowElements = tableElements.select("tbody tr");

				for (int i = 1; i < tableRowElements.size(); i++) {
					Element row = tableRowElements.get(i);
					System.out.println("Linha: " + row);
					
					Elements rowItems = row.select("td");
					System.out.println("Colunas: " + rowItems);
					
					for (int j = 0; j < rowItems.size(); j++) {
						System.out.print(rowItems.get(j).text());
					}
					System.out.println();
					
					DateFormat formatter;
					formatter = new SimpleDateFormat("dd/mm/yy");
					
					Integer concurso = Integer.valueOf(rowItems.get(0).text());
					Date dataSorteio = null;
					try {
						dataSorteio = (Date) formatter.parse(rowItems.get(1).text());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					Integer primeiraDezena = Integer.valueOf(rowItems.get(2).text());
					Integer segundaDezena = Integer.valueOf(rowItems.get(3).text());
					Integer terceiraDezena = Integer.valueOf(rowItems.get(4).text());
					Integer quartaDezena = Integer.valueOf(rowItems.get(5).text());
					Integer quintaDezena = Integer.valueOf(rowItems.get(6).text());
					Integer sextaDezena = Integer.valueOf(rowItems.get(7).text());
					Float arrecadacaoTotal = Float.valueOf(rowItems.get(8).text().replace(".", "").replace(",", "."));
					Integer ganhadoresSena = Integer.valueOf(rowItems.get(9).text());
					Float rateioSena = Float.valueOf(rowItems.get(10).text().replace(".", "").replace(",", "."));
					Integer ganhadoresQuina = Integer.valueOf(rowItems.get(11).text());
					Float rateioQuina = Float.valueOf(rowItems.get(12).text().replace(".", "").replace(",", "."));
					Integer ganhadoresQuadra = Integer.valueOf(rowItems.get(13).text());
					Float rateioQuadra = Float.valueOf(rowItems.get(14).text().replace(".", "").replace(",", "."));
					String acumulado = rowItems.get(15).text();
					Double valorAcumulado = Double.valueOf(rowItems.get(16).text().replace(".", "").replace(",", "."));
					Double estimativaPremio = Double.valueOf(rowItems.get(17).text().replace(".", "").replace(",", "."));
					Double acumuladoNatal = Double.valueOf(rowItems.get(18).text().replace(".", "").replace(",", "."));

					Sorteio sorteio = new Sorteio(concurso, dataSorteio,
							primeiraDezena, segundaDezena, terceiraDezena,
							quartaDezena, quintaDezena, sextaDezena,
							arrecadacaoTotal, ganhadoresSena, rateioSena,
							ganhadoresQuina, rateioQuina, ganhadoresQuadra,
							rateioQuadra, acumulado, valorAcumulado,
							estimativaPremio, acumuladoNatal);

					administradorSorteios.cadastrar(sorteio);
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			
/*			
			try {
				//Create object of FileReader
			    FileReader inputFile = new FileReader(fileName);
			    
			  //Instantiate the BufferedReader Class
			    BufferedReader bufferReader = new BufferedReader(inputFile);

			    //Variable to hold the one line data
			    String line;

			    // Read file line by line and print on the console
			    while ((line = bufferReader.readLine()) != null)   {
			            System.out.println(line);
			    }
			    //Close the buffer reader
			    bufferReader.close();
			    
			    
			    
			} catch (Exception e) {
				 System.out.println("Error while reading file line by line:" + e.getMessage());
				e.printStackTrace();
			}
*/			
			
		}
		/*
		 String html =
		 "http://publib.boulder.ibm.com/infocenter/iadthelp/v7r1/topic/" +
		 "com.ibm.etools.iseries.toolbox.doc/htmtblex.htm";
		  
		 try { Document doc2 = Jsoup.connect(html).get(); Elements
		 tableElements = doc2.select("table");
		 
		 Elements tableHeaderEles = tableElements.select("thead tr th");
		 System.out.println("headers"); for (int i = 0; i <
		 tableHeaderEles.size(); i++) {
		 System.out.println(tableHeaderEles.get(i).text()); }
		 System.out.println();
		  
		 Elements tableRowElements = tableElements.select(":not(thead) tr");
		  
		 for (int i = 0; i < tableRowElements.size(); i++) { Element row =
		 tableRowElements.get(i); System.out.println("row"); Elements rowItems
		 = row.select("td"); for (int j = 0; j < rowItems.size(); j++) {
		 System.out.println(rowItems.get(j).text()); } System.out.println(); }
		 
		 } catch (IOException e) { e.printStackTrace(); }
		 */
	}
}
